package com.lastingwar.mall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class MallCorsConfiguration {

    @Bean
    //需要添加的组件 跨域过滤器
    public CorsWebFilter corsWebFilter(){
        //包里面给到的实现CorsConfigurationSource接口的类
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //Cors跨域配置类，需要添加配置信息
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //1、配置跨域
        corsConfiguration.addAllowedHeader("*");//允许跨域的请求头
        corsConfiguration.addAllowedMethod("*");//允许跨域的方法
        corsConfiguration.addAllowedOrigin("*");//允许跨域的请求来源
        corsConfiguration.setAllowCredentials(true);//允许携带cookie进行跨域

        //UrlBasedCorsConfigurationSource类中添加需要跨域的路径，和跨域配置
        source.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(source);
    }
}